package com.bovink.customalbum.album;

/**
 * @author Retina975
 * @since 2017/07/05
 */

import android.content.Context;
import android.support.v7.widget.AppCompatRadioButton;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bovink.customalbum.R;
import com.bumptech.glide.Glide;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import static com.bovink.customalbum.album.AlbumUtils.imageFilter;

/**
 * 相册适配器
 */
public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.FolderHolder> {
    private Context context;
    private List<ImageFolder> imageFolders;
    /**
     * 当前选择的相册
     */
    private int currentAlbum = 0;

    public AlbumAdapter(Context context, List<ImageFolder> imageFolders) {
        this.context = context;
        this.imageFolders = imageFolders;
    }

    @Override
    public AlbumAdapter.FolderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AlbumAdapter.FolderHolder(LayoutInflater.from(context).inflate(R.layout.item_recycler_folder, parent, false));
    }

    @Override
    public void onBindViewHolder(AlbumAdapter.FolderHolder holder, final int position) {
        ImageFolder folder = imageFolders.get(position);

        // 加载相册的Logo
        Glide.with(context)
                .load(folder.getLogo())
                .into(holder.folderLogoIv);

        // 加载相册的名字
        holder.folderNameTv.setText(folder.getName());
        // 加载相册是否被选中
        holder.selectedAlbumRb.setChecked(position == currentAlbum);
        if (position == currentAlbum) {
            holder.selectedAlbumRb.setVisibility(View.VISIBLE);
        } else {
            holder.selectedAlbumRb.setVisibility(View.GONE);
        }

        // 加载相册中图片的数量
        File file = new File(folder.getPath());
        List<String> strings = Arrays.asList(file.list(imageFilter()));
        holder.folderImgNumTv.setText(strings.size() + "张");

        // 添加相册点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 保存当前选中的相册
                currentAlbum = position;
                notifyDataSetChanged();
                // 将事件传递给AlbumActivity
                AlbumActivity.relay.accept(new EventType.SelectAlbumEvent(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return imageFolders.size();
    }

    /**
     * 相册视图支撑类
     */
    class FolderHolder extends RecyclerView.ViewHolder {
        /**
         * 相册的Logo
         */
        ImageView folderLogoIv;
        /**
         * 相册的名字
         */
        TextView folderNameTv;
        /**
         * 相册中存放的图片数量
         */
        TextView folderImgNumTv;
        /**
         * 相册是否被选中
         */
        AppCompatRadioButton selectedAlbumRb;

        FolderHolder(View itemView) {
            super(itemView);

            folderLogoIv = (ImageView) itemView.findViewById(R.id.iv_folder_logo);
            folderNameTv = (TextView) itemView.findViewById(R.id.tv_folder_name);
            folderImgNumTv = (TextView) itemView.findViewById(R.id.tv_folder_img_num);
            selectedAlbumRb = (AppCompatRadioButton) itemView.findViewById(R.id.rb_album_selected);
        }
    }
}
